package com.train.springboot.springboottrain.config;


import com.train.springboot.springboottrain.filter.Myfilter2;
import com.train.springboot.springboottrain.servlet.MyServlet;
import com.train.springboot.springboottrain.servlet.MyServlet2;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.CharacterEncodingFilter;


//通过配置类注册组件
@Configuration  //该注解将此类定义为配置类（相当于一个XML配置文件）
public class ServletConfig {

/*

    //设置字符编码过滤器，设置配置文件
    @Bean
    public FilterRegistrationBean myFilterRegistrationBean(){

        //创建字符编码过滤器
        CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
        //设置强制使用指定字符编码
        characterEncodingFilter.setForceEncoding(true);
        //设置指定字符编码
        characterEncodingFilter.setEncoding("utf-8");


        //注册过滤器
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();

        //设置字符编码过滤器
        filterRegistrationBean.setFilter(characterEncodingFilter);
        //设置字符编码过滤器路径
        filterRegistrationBean.addUrlPatterns("/*");
//        设置完之后，需要在springboot配置文件中，关闭 springboot的http字符编码支持
        return filterRegistrationBean;

    }
*/

    //@Bean 是一个方法级别上的注解，主要用在配置类中
    //相当于
//        <beans>
//                <bean id="" class>
//        </beans>
    @Bean
    public ServletRegistrationBean myServletRegistrationBean(){
            ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new MyServlet2(),"/myservlet2");

        return servletRegistrationBean;
    }



}
